package com.wu.creditmanager;

import java.util.Calendar;
import java.util.List;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.widget.RemoteViews;

import com.wu.creditmanager.db.CreditsDbAdapter;
import com.wu.creditmanager.entity.Bill;

public class AlarmListWidget extends AppWidgetProvider {
	private static final String ACTION_UPDATE = "com.wu.creditmanager.APPWIDGET_UPDATE";
	private static final String TAG = null;

	public static Boolean hasWidget = false;

	@Override
	public void onEnabled(Context context) {
		synchronized (hasWidget) {
			hasWidget = true;
		}
		super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		synchronized (hasWidget) {
			hasWidget = false;
		}
		super.onDisabled(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		synchronized (hasWidget) {
			hasWidget = true;
		}
		Intent service = new Intent(context, WidgetUpdateService.class);
		context.startService(service);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		if (intent.getAction().equals(ACTION_UPDATE)) {
			synchronized (hasWidget) {
				hasWidget = true;
			}
			RemoteViews remoteView = getUpdatedView(context);
			AppWidgetManager.getInstance(context).updateAppWidget(
					new ComponentName(context, AlarmListWidget.class),
					remoteView);
		} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			Intent intent2 = new Intent(context, BillService.class);
			context.startService(intent2);
		}
	}

	public static void update(Context context) {
		context.sendBroadcast(new Intent(ACTION_UPDATE));
		Log.d(TAG, "update widget success");
	}

	private RemoteViews getUpdatedView(Context context) {
		CreditsDbAdapter adapter = new CreditsDbAdapter(context);
		adapter.open();
		List<Bill> bills = adapter.fetchAllNotPaidBills();
		adapter.close();
		RemoteViews remoteView = new RemoteViews(context.getPackageName(),
				R.layout.credit_widget_alarmlist);
		if (bills.size() > 0) {
			Bill bill = bills.get(0);
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			remoteView.setTextViewText(R.id.widget_id_credit_name, bill
					.getCredit().getName());
			remoteView.setImageViewResource(R.id.widget_id_icon, bill
					.getCredit().getBank().getIconResId());
			Resources resources = context.getResources();
			long leftdays = ((bill.getPaymentDueDay() - calendar
					.getTimeInMillis()) / 1000 / 60 / 60 / 24) + 1;
			String leftdayStr = leftdays >= 0 ? String.format(
					resources.getString(R.string.leftdays), leftdays) : String
					.format(resources.getString(R.string.leftdays2), -leftdays);
			remoteView.setTextViewText(R.id.widget_id_left_days, leftdayStr);
		} else {
			remoteView.setTextViewText(R.id.widget_id_credit_name, context
					.getResources().getString(R.string.no_not_paid_bills));
			remoteView.setTextViewText(R.id.widget_id_left_days, ""); 
		}
		Intent intent = new Intent(context, MainTabActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intent, 0);
		remoteView.setOnClickPendingIntent(R.id.widget_id_frame, pendingIntent);
		return remoteView;
	}
}
